#include <stdio.h>
#include <stdlib.h>

int main()
{
    int t = 0;
    scanf("%d", &t);
    while(t--)
    {
        int n = 0;
        scanf("%d", &n);
        char arr[n+1];
        scanf("%s", arr);

        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            int count1 = 0;
            int count2 = 0;
            for(int j = i; j < n; j++)
            {
                if(arr[j] == '(')
                {
                    count1++;
                }
                else
                {
                    if(count1 > 0)
                    count1--;
                    else
                    count2++;
                }
                sum += count1 > count2 ? count1 : count2;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}